package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.entity.UserRecord;
import com.sise.parkinglot.mapper.UserRecordMapper;
import com.sise.parkinglot.service.IUserRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRecord> implements IUserRecordService {

    @Resource
    private UserRecordMapper userRecordMapper;

    @Override
    public CodeMsg<List<UserRecord>> queryUserRecordList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<UserRecord>>().setCode(400).setMsg("参数缺失");
        }
        Page<UserRecord> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<UserRecord> userRecordQueryWrapper = new QueryWrapper<>();
        if (map.get("id") != null && !map.get("id").equals("")) {
            userRecordQueryWrapper.eq("id", map.get("id"));
        }
        if (map.get("userId") != null && !map.get("userId").equals("")) {
            userRecordQueryWrapper.eq("user_id", map.get("userId"));
        }
        if (map.get("msg") != null && !map.get("msg").equals("")) {
            userRecordQueryWrapper.like("msg", map.get("msg"));
        }
        if (map.get("recordTime") != null && !map.get("recordTime").equals("")) {
            String recordTime = (String) map.get("recordTime");
            userRecordQueryWrapper.likeRight("record_time", recordTime.split(" ")[0]);
        }
        userRecordQueryWrapper.lambda().orderByDesc(UserRecord::getRecordTime);
        IPage<UserRecord> userRecordPage = userRecordMapper.selectPage(page, userRecordQueryWrapper);
        List<UserRecord> userRecordList = userRecordPage.getRecords();
        return new CodeMsg<List<UserRecord>>().setCode(200).setMsg(String.valueOf(userRecordPage.getTotal())).setData(userRecordList);
    }
}
